package com.jgssp.ad.sdk;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.jgssp.ad.config.TTAdManagerHolder;
import com.jgssp.ad.constant.AdConstant;
import com.jgssp.ad.utils.TToast;
import com.jgssp.ad.utils.UIUtils;

import java.util.List;

/**
 * @author songzi
 * @date 2022/12/27
 */
public class BannerActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "JgAds" + BannerActivity.class.getSimpleName();
    private FrameLayout mBannerContainer;

    //广告位id
    private String mAdUnitId = AdConstant.BANNER;
    //广告是否加载成功了
    private boolean mIsLoaded;
    //广告加载成功并展示
    private boolean mIsLoadedAndShow;

    private Button mButtonDownloadShow;
    private Button mButtonDownload;
    private Button mButtonShow;

    private TTAdNative mTTAdNative;
    private TTNativeExpressAd mTTAd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);

        //step2:创建TTAdNative对象，createAdNative(Context context) banner广告context需要传入Activity对象

        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
        //step3:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(this);

        mButtonDownloadShow = findViewById(R.id.btn_download_show);
        mButtonDownload = findViewById(R.id.btn_download);
        mButtonShow = findViewById(R.id.btn_show);
        //放置banner的父容器
        mBannerContainer = findViewById(R.id.banner_container);
        initListener();
        initAdLoader();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void initAdLoader() {

    }

    /**
     * 清除状态
     */
    private void clearStatus() {
        //重置load标识
        mIsLoaded = false;
        //清空banner父容器
        mBannerContainer.removeAllViews();
    }

    @Override
    public void initListener() {
        mButtonDownloadShow.setOnClickListener(this);
    }

    private void loadExpressAd(String codeId, int expressViewWidth, int expressViewHeight) {
        mBannerContainer.removeAllViews();
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档

        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) //广告位id
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight) //期望模板广告view的size,单位dp
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理

        mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                TToast.show(BannerActivity.this, "load error : " + code + ", " + message);
                mBannerContainer.removeAllViews();
            }

            @Override

            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0) {
                    return;
                }
                /*******************
                 * 如果旧广告对象不使用了，在替换成新广告对象前，必须进行销毁，否则可能导致多个广告对象同时存在，影响SSR
                 */
                if (mTTAd != null) {
                    mTTAd.destroy();
                }
                /********************/


                mTTAd = ads.get(0);
                mTTAd.setSlideIntervalTime(30 * 1000);
                bindAdListener(mTTAd);
                TToast.show(BannerActivity.this, "load success!");
            }
        });
    }

    private void bindAdListener(TTNativeExpressAd ad) {

        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override

            public void onAdClicked(View view, int type) {
                TToast.show(BannerActivity.this, "广告被点击");
            }

            @Override

            public void onAdShow(View view, int type) {
                TToast.show(BannerActivity.this, "广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                TToast.show(BannerActivity.this, msg + " code:" + code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                TToast.show(BannerActivity.this, "渲染成功");
            }
        });

        mBannerContainer.removeAllViews();
        mBannerContainer.addView(mTTAd.getExpressAdView());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_download_show: //加载并展示插全屏广告
                loadExpressAd(mAdUnitId, UIUtils.getScreenWidthInPx(this), UIUtils.getScreenWidthInPx(this) * 100 / 640);
                break;
        }
    }
}
